package com.example.shishen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.shishen.entity.ShishenInfo;
import com.example.shishen.service.ShishenInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 式神信息Controller
 * @author Trae AI
 */
@RestController
@RequestMapping("/shishen")
public class ShishenInfoController {

    @Autowired
    private ShishenInfoService shishenInfoService;

    /**
     * 新增式神信息
     */
    @PostMapping
    public boolean save(@RequestBody ShishenInfo shishenInfo) {
        return shishenInfoService.save(shishenInfo);
    }

    /**
     * 更新式神信息
     */
    @PutMapping
    public boolean update(@RequestBody ShishenInfo shishenInfo) {
        return shishenInfoService.updateById(shishenInfo);
    }

    /**
     * 删除式神信息
     */
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id) {
        return shishenInfoService.removeById(id);
    }

    /**
     * 批量删除式神信息
     */
    @PostMapping("/batchDelete")
    public boolean batchDelete(@RequestBody List<Integer> ids) {
        return shishenInfoService.removeByIds(ids);
    }

    /**
     * 查询单个式神信息
     */
    @GetMapping("/{id}")
    public ShishenInfo getById(@PathVariable Integer id) {
        return shishenInfoService.getById(id);
    }

    /**
     * 查询所有式神信息
     */
    @GetMapping
    public List<ShishenInfo> findAll() {
        return shishenInfoService.list();
    }

    /**
     * 分页查询式神信息
     */
    @GetMapping("/page")
    public Page<ShishenInfo> findPage(@RequestParam Integer pageNum,
                                      @RequestParam Integer pageSize) {
        return shishenInfoService.page(new Page<>(pageNum, pageSize));
    }

    /**
     * 条件查询式神信息
     */
    @GetMapping("/search")
    public List<ShishenInfo> search(@RequestParam(required = false) String name,
                                  @RequestParam(required = false) String rarity,
                                  @RequestParam(required = false) String shishenType) {
        QueryWrapper<ShishenInfo> queryWrapper = new QueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("name", name);
        }
        if (rarity != null && !rarity.isEmpty()) {
            queryWrapper.eq("rarity", rarity);
        }
        if (shishenType != null && !shishenType.isEmpty()) {
            queryWrapper.eq("shishen_type", shishenType);
        }
        return shishenInfoService.list(queryWrapper);
    }
}